#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<unordered_map>

class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n = nums.size();
        vector<vector<int>> dp(n, vector<int>(n, 2));
        unordered_map<int, int> hash;
        hash[nums[0]] = 0;
        int ret = 2;
        for (int i = 1; i < n; i++)A
        {
            for (int j = i + 1; j < n; j++)
            {
                int a = 2 * nums[i] - nums[j];
                if (hash.count(a))
                    dp[i][j] = dp[hash[a]][i] + 1;
                ret = max(ret, dp[i][j]);
                
            }
            }
            hash[nums[i]] = i;
        }
        return ret;
    }
};



class Solution {
public:
    int longestSubsequence(vector<int>& arr, int difference) {
        unordered_map<int, int> hash;
        vector<vector<int>> dp(n, vector<int>(n, 2));
        hash[arr[0]] = 1;
        int n = arr.size();
        int ret = 1;
        for (int i = 1; i < n; i++)
        {
            hash[arr[i]] = hash[arr[i] - difference] + 1;
            ret = max(ret, dp[i][j]);
        }
        return ret;
    }
};

class Solution {
public:
    int longestArithSeqLength(vector<int>& nums) {
        int n = nums.size();
        vector<vector<int>> dp(n, vector<int>(n, 2));
        unordered_map<int, int> hash;
        hash[nums[0]] = 0;
        int ret = 2;
        for (int i = 1; i < n; i++)A
        {
            for (int j = i + 1; j < n; j++)
            {
                int a = 2 * nums[i] - nums[j];
                if (hash.count(a))
                    dp[i][j] = dp[hash[a]][i] + 1;
                ret = max(ret, dp[i][j]);

            }
        }
        hash[nums[i]] = i;
    }
    return ret;
}
};